import CommonAction from '../Common/CommonAction'
import Common from '../Common/common'
import Tween = Laya.Tween;
import { Data } from '../Data/data';
export default class Home extends Laya.Scene {
    [x: string]: any;
    //isToutiao: boolean = true
    isBind: boolean = false


    constructor() {
        super()

        Laya.stage.alignV = "middle";
        //水平居中对齐，另一种写法：Laya.stage.alignH = Stage.ALIGN_CENTER;
        Laya.stage.alignH = "center";

        Laya.Event.TRIGGER_ENTER
    }

    /**
     * 监听打开场景
     */
    onOpened() {
        Laya.stage.event("showBannerAd");
        if (Common.getCacheItem("musicSetting") == "0") {
            this.music.skin = "images/index/closeMusic.png"
        } else {
            this.music.skin = "images/index/openMusic.png"
        }
        // let wxLogin = new CommonAction()
        // wxLogin.getToken()
        // wxLogin.commonBtnEvent()
        Laya.stage.event("playMusic", ["sound/bg.mp3", true])
        this.btnEvent()
        this.isSignIn()
    }

    /**
     * 按钮监听
     */
    btnEvent() {
        Laya.stage.on("startGame", this, this.startGame);
        Laya.stage.on("openProp", this, this.openProp)
        Laya.stage.on("openRank", this, this.openRank)
        Laya.stage.on("controlMusic", this, this.controlMusic)

        if (Common.platform == "weixin" && Common.getCacheItem("nick") == null) {//如果是微信且没有授权直接渲染透明按钮
            console.log("微信授权")
            Laya.stage.event("Authorize", [this.playButton, "startGame"]);//渲染获取用户信息的按钮
            Laya.stage.event("Authorize", [this.gameProp, "openProp"]);
            Laya.stage.event("Authorize", [this.rankList, "openRank"]);
            Laya.stage.event("Authorize", [this.music, "controlMusic"]);
            //this.isToutiao = false
        } else if (Common.platform == "toutiao" && Common.getCacheItem("nick") == null) {//头条未授权的情况下
            //绑定点击事件
            this.gameProp.on(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                Common.lastAuthCallbackEventName = "openProp";
                Laya.stage.event("Authorize");//获取授权信息
            });
            this.playButton.on(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                Common.lastAuthCallbackEventName = "startGame";
                Laya.stage.event("Authorize");//获取授权信息
            });
            this.rankList.on(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                Common.lastAuthCallbackEventName = "openRank";
                Laya.stage.event("Authorize");//获取授权信息
            });
            this.music.on(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                Common.lastAuthCallbackEventName = "controlMusic";
                Laya.stage.event("Authorize");//获取授权信息
            });
           // this.isToutiao = true
        } else {//直接可以点击
            this.bindBtn()

        }
    }

    /**
     * 是否签到
     */
    isSignIn() {
        let lastSignDate = Common.getCacheItem("signDate");
        if (lastSignDate != Common.getNowDate() || lastSignDate == null) {
            Laya.Scene.open("Dialog/signIn.scene")
        }
    }

    controlMusic() {
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        // if (!this.isBind && !this.isToutiao) {
        //     this.bindBtn()
        //     this.isBind = true
        // }
        if (Common.getCacheItem("musicSetting") == "1" || Common.getCacheItem("musicSetting") == null) {
            this.music.skin = "images/index/closeMusic.png"
            Common.setCacheItem("musicSetting", "0")
            console.log("关闭音乐")
            Laya.stage.event("stopMusic")
        } else {
            this.music.skin = "images/index/openMusic.png"
            Common.setCacheItem("musicSetting", "1")
            console.log("打开音乐")
            Laya.stage.event("playMusic", ["sound/bg.mp3", true])
        }
        // if(this.isToutiao){
        //     this.cleanAuthEvent()
        //     this.bindBtn();//重新渲染其他按钮
        // }
    }

    startGame() {
        console.log(new Date(),"1233")
        Laya.stage.event("hideBannerAd");
        Laya.Scene.open("gameView.scene");
        Laya.stage.event("playSound", ["sound/tap.mp3"])
    }
    openProp() {
        Laya.stage.event("hideBannerAd");
        Laya.Scene.open("gameProp.scene", false)
        Laya.stage.event("playSound", ["sound/tap.mp3"])
    }
    openRank() {
        Laya.stage.event("hideBannerAd");
        Laya.Scene.open("rankingList.scene", false, { type: "home" })
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        this.bindBtn();
    }

    cleanAuthEvent() {
        this.playButton.off(Laya.Event.CLICK, this);//清除之前的事件
        this.gameProp.off(Laya.Event.CLICK, this)//清除之前的事件
        this.rankList.off(Laya.Event.CLICK, this)//清除之前的事件
        //this.music.off(Laya.Event.CLICK, this)//清除之前的事件
    }
    /***
     * 绑定btn
     */
    bindBtn() {
        this.playButton.on(Laya.Event.CLICK, this, this.startGame);
        this.gameProp.on(Laya.Event.CLICK, this, this.openProp)
        this.rankList.on(Laya.Event.CLICK, this, this.openRank)
        this.music.on(Laya.Event.CLICK, this, this.controlMusic)
    }


}